//
//  ViewController.m
//  BaiduMap
//
//  Created by Lucy_Lee on 2017/1/13.
//  Copyright © 2017年 Lucy_Lee. All rights reserved.
//

#import "ViewController.h"
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Map/BMKPointAnnotation.h>
#import <BaiduMapAPI_Map/BMKPinAnnotationView.h>
#import <BaiduMapAPI_Map/BMKPolyline.h>
#import <BaiduMapAPI_Map/BMKPolylineView.h>

@interface ViewController ()<BMKMapViewDelegate>

@property (nonatomic, strong) BMKMapView * mapView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _mapView = [[BMKMapView alloc]initWithFrame:self.view.bounds];
    self.view = _mapView;
    [_mapView setMapType:BMKMapTypeStandard];//3种类型（普通矢量地图、卫星图和空白地图）
    [_mapView setTrafficEnabled:YES];//打开实时路况图层
//    [_mapView setBaiduHeatMapEnabled:YES];//打开百度城市热力图图层（百度自有数据）
    _mapView.showMapScaleBar = YES;
//    [_mapView setShowMapPoi:NO];//将底图标注设置为隐藏
   
    // Do any additional setup after loading the view, typically from a nib.
    
    /*
     几何图形
     */
//    CLLocationCoordinate2D coors[2] = {0};
//    coors[0].latitude = 39.315;
//    coors[0].longitude = 116.304;
//    coors[1].latitude = 39.515;
//    coors[1].longitude = 116.504;
//    BMKPolyline * polyline = [BMKPolyline polylineWithCoordinates:coors count:2];
//    [_mapView addOverlay:polyline];
    
    CLLocationCoordinate2D coors[4] = {0};
    coors[0].latitude = 39.965;
    coors[0].longitude = 116.404;
    coors[1].latitude = 39.925;
    coors[1].longitude = 116.454;
    coors[2].latitude = 39.955;
    coors[2].longitude = 116.494;
    coors[3].latitude = 39.905;
    coors[3].longitude = 116.654;
    //构建分段纹理索引数组
    NSArray * textureIndex = [NSArray arrayWithObjects:@2,@0,@1, nil];
    BMKPolyline * polyline = [BMKPolyline polylineWithCoordinates:coors count:4 textureIndex:textureIndex];
    [_mapView addOverlay:polyline];
    
}

-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay{
    
    if ([overlay isKindOfClass:[BMKPolyline class]]) {
        BMKPolylineView * polylineView = [[BMKPolylineView alloc]initWithOverlay:overlay];
//        polylineView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1.0f];
        polylineView.isFocus = YES;
        polylineView.colors = [NSArray arrayWithObjects:[UIColor redColor],[UIColor blueColor],[UIColor purpleColor], nil];
        polylineView.lineWidth = 2.0f;
        return polylineView;
    }
    return nil;
}

-(void)viewDidAppear:(BOOL)animated{
    /*
    百度地图SDK也预置了基本的标注点（BMKPointAnnotation）和一个大头针标注View（BMKPinAnnotationView），您可以直接使用来显示标注
     */
    BMKPointAnnotation * annotation = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor;
    coor.latitude = 39.915;
    coor.longitude = 116.404;
    annotation.coordinate = coor;
    annotation.title = @"BEIJING";
    [_mapView addAnnotation:annotation];
    /*
     相应的删除标注方法
     */
//    if (annotation != nil) {
//        [_mapView removeAnnotation:annotation];
//    }

}

-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView * annotationView = [[BMKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"lee_annotation"];
        annotationView.pinColor = BMKPinAnnotationColorRed;
        annotationView.animatesDrop = YES;
        return annotationView;
    }
    return nil;
}

-(void)viewWillAppear:(BOOL)animated{
    [self.mapView viewWillAppear];
    self.mapView.delegate = self;
}

-(void)viewWillDisappear:(BOOL)animated{

    [self.mapView viewWillDisappear];
    self.mapView.delegate = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
